package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Problem;
import cn.wolfcode.luowowo.article.query.ProblemCommendQuery;
import cn.wolfcode.luowowo.article.service.IProblemService;
import cn.wolfcode.luowowo.cache.service.IProblemStatisService;
import cn.wolfcode.luowowo.comment.domain.Prob;
import cn.wolfcode.luowowo.comment.domain.ProblemComment;
import cn.wolfcode.luowowo.comment.service.IProblemCommentService;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/problem")
public class ProblemController {
    @Reference
    private IProblemCommentService problemCommentService;
    @Reference
    private IProblemService problemService;
    @Reference
    private IProblemStatisService problemStatisService;

    @RequestMapping("commentAdd")
    @ResponseBody    //初始化集合
    public String commentAdd(ProblemComment comment,@GetUserInfoAnnotation UserInfo userInfo){
        comment.setProbId(1L);
        comment.setCity("广州");
        comment.setContent("jkhjkghkhknk");
        comment.setCreateTime(new Date());
        comment.setProbTitle("chichcih");
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        problemCommentService.commentAdd(comment);
        return "ok";
    }



    @RequestMapping("") //问答首页
    public String wenda(Model model,ProblemCommendQuery qo){
        //问题显示
        PageInfo pageInfo = problemService.query(qo);
        List<Problem> list = pageInfo.getList();
        for (Problem problem : list) {
            List<String> arrlist = problem.getArrlist();
            String tag = problem.getTag();
            String[] split = tag.split(",");
            for (String s : split) {
                arrlist.add(s);
            }
            problem.setArrlist(arrlist);
        }
        model.addAttribute("pageInfo",pageInfo);
        List<Prob> plist =problemCommentService.getshuju(1L);
        model.addAttribute("f",plist);
        return "/wenda/wenda";
    }



    @RequestMapping("savaOrUpdate")
    @ResponseBody
    public Object sava(Problem problem,@GetUserInfoAnnotation UserInfo userInfo){
        problem.setLevel(userInfo.getLevel());
        problem.setHeadUrl(userInfo.getHeadImgUrl());
        problem.setUserId(userInfo.getId());
        problem.setUsername(userInfo.getNickname());
        problemService.savaOrUpdate(problem);
        return new AjaxResult();
    }


    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@GetUserInfoAnnotation UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "您还没登录");
            result.setCode(102);
            return result;
        }
        boolean b = problemStatisService.addFavor(sid,userInfo.getId());
        int co = problemStatisService.getCount(sid);
        return new AjaxResult(b,null).addData(co);
    }
}
